package cn.com.shopec.erp.stock.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.orm.impl.HibernateEntityDao;
import cn.com.shopec.erp.stock.commo.ObjectUtil;
import cn.com.shopec.erp.stock.dao.IStockOutProductDao;
import cn.com.shopec.erp.stock.entity.ProductBarCode;
import cn.com.shopec.erp.stock.entity.StockOutProduct;
import cn.com.shopec.erp.stock.vo.StockOutProductVo;

/**
 * 
 * @ClassName: StockOutProductImpl.java
 * @Description: 出库商品Dao接口实现类
 * @author: zengxuezhou
 * @date: 2016年11月28日 下午2:25:18
 */
@Repository
public class StockOutProductImpl extends HibernateEntityDao<StockOutProduct> implements IStockOutProductDao {

	@Override
	public void removeProductByStockOutNo(String stockOutNo) {

		// String hql = "delete from StockOutProduct as p where p.stockOutNo =
		// ?";O
		String hql = "update StockOutProduct as p set deleteFlag = " + SZSCMConstant.DELETE_FLAG_YES
				+ " where p.stockOutNo = ?";
		getSession().createQuery(hql).setString(0, stockOutNo).executeUpdate();
	}
	
	@Override
	public boolean removeForUpdate(String stockOutNo) {
		DetachedCriteria criteria = createCriteria();
		criteria.add(Restrictions.eq("deletedFlag", SZSCMConstant.DELETE_FLAG_NO));
		criteria.add(Restrictions.eq("stockOutNo", stockOutNo));
		List<StockOutProduct> stockOutProducts = findByCriteria(criteria);// 查询入库单产品信息
		if (!CollectionUtils.isEmpty(stockOutProducts)) {
			StringBuilder hql = new StringBuilder("update StockOutProduct as p set p.deletedFlag = "
					+ SZSCMConstant.DELETE_FLAG_YES + " where p.id in (");
			for (StockOutProduct stockOutProduct : stockOutProducts) {
				hql.append(" '" + stockOutProduct.getId() + "',");
			}
			hql.deleteCharAt(hql.length() - 1);
			hql.append(" )");
			return getSession().createQuery(hql.toString()).executeUpdate() > 0;
		}
		return true;
	}

	@SuppressWarnings({ "unchecked"})
	@Override
	public List<StockOutProductVo> getProductsListByStockNo(String stockNo, int pageCount, int pageSize) {
		String sql = "SELECT S.ID AS id,"
				+ " S.STOCKOUT_NO AS stockOutNo,"
				+ " S.PROD_NO AS prodNo,"
				+ " S.COMMO_NO AS commoNo,"
				+ " S.SUP_NO AS supNo,"
				+ " S.PUR_NO AS purNo,"
				+ " S.PUR_PROD_NO AS purProdNo,"
				+ " S.WH_NO AS whNo, "
				+ " S.PROD_DESC AS prodDesc,"
				+ " S.STOCKOUT_QUANTITY AS stockoutQuantity,"
				+ " S.PRODUCT_CODE_STATE AS productCodeState,"
				+ " TW.WH_NAME AS whName,"
				+ " TSI.SUP_NAME AS supName,"
				+ " C.COMMO_NAME AS commoName,"
				+ " SVO.OPTION_NAME AS specification,"
				+ " PROD.PROD_BAR_CODE AS prodBarCode,"
				+ " TPO.PONAME AS purName,"
				+ " TPS.IS_SUP_DELIVERY AS isSupDelivery"
				+ " FROM TBL_STOCKOUT_PRODUCT S"
				+ " LEFT JOIN TBL_WAREHOUSE TW ON TW.WH_NO = S.WH_NO"
				+ " LEFT JOIN TBL_SUPPLIER_INFO TSI ON TSI.CODE = S.SUP_NO"
				+ " LEFT JOIN TBL_PRODUCT PROD ON PROD.PROD_NO = S.PROD_NO"
				+ " LEFT JOIN TBL_COMMODITY C ON C.COMMO_NO = S.COMMO_NO "
				+ " LEFT JOIN TBL_PURCHASE_ORDER TPO ON TPO.PO_NO = S.PUR_NO"
				+ " LEFT OUTER JOIN TBL_PROD_SPEC_VALUE PSV ON PSV.PROD_NO = S.PROD_NO"
				+ " LEFT OUTER JOIN TBL_SPEC_VAL_OPTION SVO ON SVO.OPTION_NO = PSV.VAL_OPTION_NO "
				+ " LEFT OUTER JOIN TBL_PROD_SUPPLIER TPS ON TPS.SUPPLIER_NO = S.SUP_NO AND TPS.PROD_NO = S.PROD_NO"
				+ " WHERE S.STOCKOUT_NO = ? GROUP BY S.ID";
		Query query = getSession().createSQLQuery(sql)
				.addScalar("id", StandardBasicTypes.STRING)
				.addScalar("stockOutNo", StandardBasicTypes.STRING)
				.addScalar("prodNo", StandardBasicTypes.STRING)
				.addScalar("commoNo", StandardBasicTypes.STRING)
				.addScalar("supNo", StandardBasicTypes.STRING)
				.addScalar("purNo", StandardBasicTypes.STRING)
				.addScalar("whNo", StandardBasicTypes.STRING)
				.addScalar("prodDesc", StandardBasicTypes.STRING)
				.addScalar("stockoutQuantity", StandardBasicTypes.LONG)
				.addScalar("whName", StandardBasicTypes.STRING)
				.addScalar("supName", StandardBasicTypes.STRING)
				.addScalar("purProdNo", StandardBasicTypes.STRING)
				.addScalar("commoName", StandardBasicTypes.STRING)
				.addScalar("specification", StandardBasicTypes.STRING)
				.addScalar("productCodeState", StandardBasicTypes.INTEGER)
				.addScalar("prodBarCode", StandardBasicTypes.STRING)
				.addScalar("purName", StandardBasicTypes.STRING)
				.addScalar("isSupDelivery", StandardBasicTypes.INTEGER);
		query.setParameter(0, stockNo).setFirstResult(pageCount).setMaxResults(pageSize);
		query.setResultTransformer(Transformers.aliasToBean(StockOutProductVo.class));
		List<StockOutProductVo> stockOutProductVos = query.list();
		return stockOutProductVos;
	}

	@Override
	public int getProductsCountByStockNo(String stockNo) {
		DetachedCriteria detachedCriteria = createCriteria();
		detachedCriteria.add(Restrictions.eq("stockOutNo", stockNo));
		return getCountByCriteria(detachedCriteria);
	}
	
	@Override
	public void setProductCodeIsImport(String id, int state) {
		String hql = "update StockOutProduct as p set p.productCodeState = ? where p.id = ?";
		getSession().createQuery(hql).setInteger(0, state).setString(1, id).executeUpdate();
	}

	@Override
	public ProductBarCode getProductCodeBycode(String code) {
		DetachedCriteria detachedCriteria = createCriteria();
		detachedCriteria.add(Restrictions.eq("code", code));
		detachedCriteria.add(Restrictions.eq("effectiveState", SZSCMConstant.DELETE_FLAG_NO));
		@SuppressWarnings("unchecked")
		List<ProductBarCode> productBarCodes = (List<ProductBarCode>) pagedByCriteria(detachedCriteria);
		if(ObjectUtil.isNotNull(productBarCodes)){
			return productBarCodes.get(0);
		}
		return null;
	}

	@Override
	public void setProductCodeIsNoEffective(String id, Integer state) {
		String hql = "update ProductBarCode as p set p.effectiveState = ? where p.id = ?";
		getSession().createQuery(hql).setInteger(0, state).setString(1, id).executeUpdate();
	}
}
